package net.ak.dao.impl;

import java.util.ArrayList;
import java.util.List;

import net.ak.dao.CategoryDao;
import net.ak.model.Category;
import net.ak.model.UserCategory;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CategoryDaoImpl extends BaseDao<Category> implements CategoryDao {

	/* (non-Javadoc)
	 * @see net.ak.dao.CategoryDao#listCategories()
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<Category> listCategories() {
		Session session 			= sessionFactory.openSession();
		List<Category> categories 	= session.createCriteria(Category.class).list();
		session.close();
		return categories;
	}
	
	/* (non-Javadoc)
	 * @see net.ak.dao.CategoryDao#listCategoriesByCategoryIds(java.util.List)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<Category> listCategoriesByCategoryIds(List<UserCategory> userCategories) {
		List<Category> categories 	= new ArrayList<Category>();
		
		if (userCategories == null || userCategories.size() == 0) {
			return categories;
		}
		
		Session session 			= sessionFactory.openSession();
		Criteria cretirea 			= session.createCriteria(Category.class);
		Criterion[] c				= new Criterion[userCategories.size()];
		for (int i = 0; i < userCategories.size(); i++) {
			c[i] = Restrictions.idEq(userCategories.get(i).getCategory().getId());
		}
		cretirea.add(Restrictions.or(c));
		categories = cretirea.list();
		session.close();
		
		return categories;
	}

	/* (non-Javadoc)
	 * @see net.ak.dao.CategoryDao#getCategoryByName(java.lang.String)
	 */
	@Override
	public Category getCategoryByName(String categoryName) {
		Session session 	= sessionFactory.openSession();
		Criteria criteria 	= session.createCriteria(Category.class).add(Restrictions.eq("name", categoryName));
		Category category 	= criteria.list().size() > 0 ? (Category)criteria.list().get(0) : null;
		session.close();
		return  category;
	}

	/* (non-Javadoc)
	 * @see net.ak.dao.CategoryDao#getCategoryById(java.lang.Long)
	 */
	@Override
	public Category getCategoryById(Long id) {
		Session session 	= sessionFactory.openSession();
		Criteria criteria 	= session.createCriteria(Category.class).add(Restrictions.idEq(id));
		Category category 	= criteria.list().size() > 0 ? (Category)criteria.list().get(0) : null;
		session.close();
		return  category;
	}

	/* (non-Javadoc)
	 * @see net.ak.dao.CategoryDao#searchCategoryByName(java.lang.String)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<Category> searchCategoryByName(String categoryName) {
		Session session	 			= sessionFactory.openSession();
		Criteria criteria 			= session.createCriteria(Category.class).add(Restrictions.like("name", categoryName));
		List<Category> categories 	= criteria.list();
		
		session.close();
		return categories;
	}
}
